package game;

//This define sequence to unlock
public class Sequence {
	private int dim;
	private char digit[];
	private Set set;
	private int head;
	
	//Define what kind of sequence we play
	public Sequence(int dim, Set set){
		this.dim = dim;
		this.digit = new char[dim];
		head = 0;
		this.set = set;
	}
	
	public int getDim(){
		return dim;
	}
	
	public int getHead(){
		return head;
	}
	
	/*public void setDim(int dim){
		this.dim = dim;
	}*/
	
	//Set a digit of sequence, note that duplicate are allowed
	public boolean setDigit(int index, char digit){
		if(index > head || set.check(digit)){
			return false;
		}
		this.digit[index] = digit;
		return true;
	}
	
	//Transform a string in a sequence
	public boolean setAllDigit(String digits){
		boolean res = true;
		if(digits.length() > dim) return false;
		head = digits.length() - 1;
		for(int i = 0; i <= head; i++){
			res = setDigit(i, digits.charAt(i));
		}
		return res;
	}
	
	//Return digit in a given position, return / if position is > of head
	public char getDigit(int position){
		if(position > head) return '/';
		else return digit[position];
	}
	
	public char[] getAllDigit(){
		char[] temp = new char[head + 1];
		for (int i = 0; i <= head; i++){
			temp[i] = digit[i];
		}
		return temp;
	}
	
	//This return 0 if c is not in sequence, 1 if c is a wrong position,
	//2 if c is in right position.
	//Just for us: 0 = red; 1 = yellow, 2 = green
	public int check(char c, int position){
		int color = 0;
		int i = 0;
		int p = -1;
		while(color == 0 && i <= head){
			if(c == digit[i]) {
				color = 1;
				p = i;
			}
			else i++;
		}
		if(p == position) color = 2;
		return color;
	}
	
	//Return a sequence of 0, 1 and 2 (red, yellow and green)
	public int[] trySequence(char[] test){
		int[] temp = new int[test.length];
		for(int i = 0; i < test.length; i++){
			temp[i] = this.check(test[i], i);
		}
		return temp;
	}
}
